from .factor_model import FactorModel
from .returns_helper import ReturnsHelper


class FactorModelHelper(object):
    def __init__(self, data_path) -> None:
        self.returns_helper = ReturnsHelper(data_path)

    def get_model(self, config: dict) -> FactorModel:
        model = FactorModel()
        model.start_date = config["start_date"]
        model.end_date = config["end_date"]

        rf = self.returns_helper.get_factor(
            config["rf"]["source"], config["rf"]["index_id"]
        )
        model.set_risk_free_rate(rf)
        for f in config["factors"]:
            factor = self.returns_helper.get_factor(f["source"], f["index_id"])
            if f["source"] == "csmar_ind":
                factor.name = f["name"]
            model.add_factor_return(factor, f["adjust_rf"])

        model.calculate_cov_matrix()
        return model
